<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Pattern.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/utils/Str.php';
?>

<SCRIPT type="text/javascript" src="/view/javascripts/form.js"></SCRIPT>

	<?php
	if($response->isParameter('process')) {
		$process = $response->getParameter('process');
		$idProcess = $process->getIdentity();
		$code = $process->getCode();
		$name = $process->getName();
		$object = $process->getObject();
		$owner = $process->getOwner()->getIdentity();
		$indicators = $process->getIndicators();
	} else {
		$idProcess = null;
		$code = '';
		$name = '';
		$object = '';
		$owner = '';
		$indicators = array();
	}
	?>
	
<DIV class="block">
	<H1><A href="/editing/process/index.html">Процессы </A> &raquo; 
	Редактирование процесса ::

	<?php
		if($idProcess !== null) {
			echo $code;
	} else {
			echo "Новый процесс";
	}
?>
	</H1>
	<FORM action="/editing/process/update.html" method="post" id="editprocess">
<?php
	if($response->isParameter('process')) {
?>
		<INPUT type="hidden" name="id_process" value="<?php echo $process->getIdentity();?>">
<?php
	}
?>
		<TABLE>
			<TR>
				<TD><LABEL for="processcode">Шифр процесса:</LABEL></TD>
				<TD>
					<INPUT type="text" id="processcode" name="code"
							value="<?php echo $code;?>"
							pattern="<?php echo Pattern::PROCESS_CODE;?>"
							message="<?php echo Error::INCORRECT_PROCESS_CODE;?>"
							maxlength="20">
				</TD>
			</TR>
			<TR>
				<TD><LABEL for="processname">Название процесса:</LABEL></TD>
				<TD>
					<INPUT type="text" id="processname" name="name" size="137"
							value="<?php echo $name;?>"
							pattern="<?php echo Pattern::SENTENCE;?>"
							message="<?php echo Error::INCORRECT_PROCESS_NAME;?>"
							maxlength="250">
				</TD>
			</TR>
			<TR>
				<TD><LABEL for="processobject">Цель:</LABEL></TD>
				<TD>
					<textarea id="processobject" name="object" cols=150 rows=7 pattern="<?php echo Pattern::SENTENCE;?>" message="<?php echo Error::INCORRECT_PROCESS_OBJECT;?>"><?php echo $object;?></textarea>
				</TD>
			</TR>
			<TR>
				<TD><LABEL for="id_owner_post">Владелец:</LABEL></TD>
				<TD>
			   		<?php 
			   			if($response->isParameter('unique-posts')) {
					?>
					<SELECT id="id_owner_post" name="id_owner">
					<?php
    					foreach($response->getParameter('unique-posts') as $unique_owner) {
					?>
						<OPTION  value="<?php echo $unique_owner->getIdentity();?>" 
							<?php if (!($newprocess) && ($owner == $unique_owner->getIdentity() )){
								echo "selected";						
							}?>
						>
						<?php echo $unique_owner->getName();?>
					</OPTION>
					<?php
						}
					?>
					</SELECT>
					<?php
						}
					?>
			    </TD>
			</TR>
		</TABLE>
	</FORM>
</DIV>

<DIV class="block">
	<BUTTON type="button" onclick="submitForm('editprocess')">
		<IMG src="/view/img/button-save.png" alt="">
		<SPAN>сохранить</SPAN>
	</BUTTON>
	<BUTTON type="button" onclick="resetForm('editprocess')">
		<IMG src="/view/img/button-reset.png" alt="">
		<SPAN>сбросить</SPAN>
	</BUTTON>

<?php
	if($response->isParameter('process')) {
?>
	<FORM action="/editing/process/delete.html" method="post" class="inline">
		<INPUT type="hidden" name="id_process" value="<?php echo $process->getIdentity();?>">
		<BUTTON type="submit">
			<IMG src="/view/img/button-delete.png" alt="">
			<SPAN>удалить</SPAN>
		</BUTTON>
	</FORM>
<?php
	}
?>
	<FORM action="/editing/process/index.html" method="get" class="inline">
		<BUTTON type="submit">
			<IMG src="/view/img/button-back.png" alt="">
			<SPAN>назад</SPAN>
		</BUTTON>
	</FORM>
</DIV>

<?php 
if($response->isParameter('process')) {
?>
<DIV class="block">
	<H1>Показатели процесса</H1>
		<TABLE>
			<TR>
				<TH>Целевой</TH>
				<TH>Название</TH>
				<TH>Плановое значение</TH>
				<TH>Единица измерения</TH>
				<TH>Весовой коэффициент</TH>
				<TH>Год</TH>
			</TR>
<?php

	$size = count($indicators) - 1;
	foreach($indicators as $i => $indicator) {
		showIndicators($indicator,$i == $size);
	}
?>
		</TABLE>

	<DIV class="block">
		<FORM action="/editing/indicator/edit.html" method="post">
		<!-- передаем идентификатор показателя, для которого определяем новый показатель -->
			<INPUT type="hidden" name="id_process" value="<?php echo $process->getIdentity();?>">
			<BUTTON type="submit">
				<IMG src="/view/img/button-new.png" alt="">
				<SPAN>новый</SPAN>
			</BUTTON>
		</FORM>
	</DIV>
</DIV>


<?php 
}
?>
<?php
//----------------------------------------------------------------------------
function showIndicators(Indicator $indicator,$isLast = false, $levels = null) 
 {
	$id = $indicator->getIdentity();
	$name = $indicator->getName();
	$objective = $indicator->isObjective();
	$unitName = $indicator->getUnit()->getName();
	$weight = $indicator->getWeight();
	$year = $indicator->getYear();
	$plan = $indicator->getPlanValue();
	
	$editId = 'editindicator-'.$id;
?>
<TR onclick="submitForm('<?php echo $editId;?>')">
	<TD>
<?php
	if($objective) {
?>
		<IMG src="/view/img/check.png" alt="Да">
<?php
	} else {
?>
				&nbsp;
<?php
	}
?>
	</TD>
	<TD>
		<FORM action="/editing/indicator/edit.html" method="post"
				id="<?php echo $editId;?>">
		<INPUT type="hidden" name="id_indicator" value="<?php echo $id;?>">
		</FORM>
<?php
	if($levels !== null) {
		foreach($levels as $level) {
			if($level) {
?>
		<DIV class="image-1"></DIV>
<?php
			} else {
?>
		<DIV class="image-0"></DIV>
<?php
			}
		}
		$levels[] = !$isLast;
	} else {
		$levels = array();
	}
	if(count($levels) > 0) {
		if($isLast) {
?>
		<DIV class="image-3"></DIV>
<?php
		} else {
?>
		<DIV class="image-2"></DIV>
<?php
		}
	}
	echo $name;
?>
	</TD>
	<TD><?php if ($plan!=0){echo $plan;}?></TD>
	<TD><?php echo $unitName;?></TD>
	<TD><?php echo $weight;?></TD>
	<TD><?php echo $year;?></TD>
</TR>
<?php
	$n = count($indicator->getChildren()) - 1;
	foreach($indicator->getChildren() as $i => $child) {
		showIndicators($child, $i == $n, $levels);
	}
}
?>
